/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.slider;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;

import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.JQOptions;
import com.noocodecommit.wicket.stuff.jquery.exception.CannotApplyJQOptionException;
import com.noocodecommit.wicket.stuff.jquery.plugin.IJQPluginComponent;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * Component used to design sliders<br>
 * Jquery use for this behavior is <code>$('#id').slider(options);</code><br>
 * With Wicket, you just need to create a JQSlider Component
 * 
 * @author Benoit Bouchez
 * 
 */
public class JQSlider extends Panel implements IJQPluginComponent
{
	/** Slider Style 1 */
	public static final String STYLE_SLIDER_1 = "ui-slider-1";
	/** Slider Style 2 */
	public static final String STYLE_SLIDER_2 = "ui-slider-2";
	private String style = STYLE_SLIDER_1;

	/** Parent */
	private WebMarkupContainer parent = new WebMarkupContainer("slider")
	{
		@Override
		protected void onComponentTag(ComponentTag tag)
		{
			super.onComponentTag(tag);
			tag.put("class", JQSlider.this.style);
		}
	};

	/** JQuery plugin */
	private JQSliderPlugin plugin;
	/** JQuery default options */
	private JQSliderOptions options = new JQSliderOptions();

	/** current focused handle component */
	private Component handle = null;
	/** current handle's value */
	private int value;
	/** the current range value (only with two handles and range: true) */
	private int range;

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component id
	 * @param range
	 *            slider range
	 * @param style
	 *            slider style
	 * @param key
	 *            plugin unique id on which component is attached
	 */
	public JQSlider(String id, boolean range, String style, String key)
	{
		super(id);
		this.setOutputMarkupId(true);
		if (key == null)
		{
			key = JQSliderPlugin.DEFAULT_KEY;
		}
		if (JQCore.get().getPlugin(key) == null)
		{
			new JQSliderPlugin(key);
		}
		this.plugin = (JQSliderPlugin) JQCore.get().getPlugin(key);
		JQCore.get().getPlugin(plugin.getKey()).attachComponentForDocumentReady(this);
		this.options.setRange(range);
		this.style = style;

		parent.setOutputMarkupId(true);
		add(parent);

		// -- Handle 1
		Label handle1 = new Label("handle1");
		handle1.setOutputMarkupId(true);
		parent.add(handle1);
		// -- Handle 2
		Label handle2 = new Label("handle2");
		handle2.setOutputMarkupId(true);
		handle2.setVisible(range);
		parent.add(handle2);
	}

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component id
	 * @param range
	 *            slider range
	 * @param style
	 *            slider style
	 */
	public JQSlider(String id, boolean range, String style)
	{
		this(id, range, style, null);
	}

	public JQSliderOptions getOptions()
	{
		return options;
	}

	public void setOptions(JQOptions options)
	{
		if (options.getClass().equals(JQSliderOptions.class))
		{
			this.options = (JQSliderOptions) options;
		}
		else
		{
			throw new CannotApplyJQOptionException(JQSlider.class, options.getClass(), JQSliderOptions.class);
		}
	}

	public JQSliderPlugin getPlugin()
	{
		return plugin;
	}

	public void setPlugin(JQPlugin plugin)
	{
		this.plugin = (JQSliderPlugin) plugin;
	}

	public String getWidgetReadyScript(boolean reset)
	{
		return "$('#" + parent.getMarkupId() + "')." + (reset ? "slider('destroy')." : "") + options.getDocumentReadyOptions();
	}

	public String getAfterAjaxUpdateScript()
	{
		return "$('#" + parent.getMarkupId() + "')." + options.getDocumentReadyOptions();
	}

	/**
	 * Enables this slider<br>
	 * Appends a javascript call <code>$('#id').slider('enable')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void enable(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('enable')");
	}

	/**
	 * Disables this slider<br>
	 * Appends a javascript call <code>$('#id').slider('disable')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void disable(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('disable')");
	}

	/**
	 * Destroy this selectable<br>
	 * Appends a javascript call <code>$('#id').slider('destroy')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void destroy(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('destroy')");
	}

	/**
	 * Gets the handle component given by the expression <code>ui.handle</code>.
	 * 
	 * @return the handle wicket component
	 */
	public Component getHandle()
	{
		return handle;
	}

	/**
	 * Sets the slider's handle component
	 * 
	 * @param handle
	 *            handling wicket component
	 */
	public void setHandle(Component handle)
	{
		this.handle = handle;
	}

	/**
	 * Gets the value given by the expression <code>ui.value</code>.
	 * 
	 * @return the slider's value
	 */
	public int getValue()
	{
		return value;
	}

	/**
	 * Sets the slider's value
	 * 
	 * @param value
	 *            slider's value
	 */
	public void setValue(int value)
	{
		this.value = value;
	}

	/**
	 * Gets the range given by the expression <code>ui.range</code>.
	 * 
	 * @return the slider's range
	 */
	public int getRange()
	{
		return range;
	}

	/**
	 * Sets the slider's range
	 * 
	 * @param range
	 *            slider's range
	 */
	public void setRange(int range)
	{
		this.range = range;
	}
}
